/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaCadastroUsuario.java
 *
 * Created on May 31, 2011, 1:06:04 PM
 */
package funcenter.gui;

import funcenter.db.DB;
import funcenter.main.Mascara;
import funcenter.main.Usuario;
import javax.swing.JOptionPane;

/**
 *
 * @author arthur
 */
public class JanelaCadastroUsuario extends javax.swing.JDialog {

    private DB db;
    
    /** Creates new form JanelaCadastroUsuario */
    public JanelaCadastroUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        db = new DB();        
        
        this.setLocationRelativeTo(parent);
        
        campoNome.setDocument(new Mascara(100, true, false));
        campoEmail.setDocument(new Mascara(100));
        campoUsuario.setDocument(new Mascara(50, false, true));
        campoSenha.setDocument(new Mascara(50));
        campoConfirmarSenha.setDocument(new Mascara(50));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelNome = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        labelEmail = new javax.swing.JLabel();
        campoEmail = new javax.swing.JTextField();
        labelUsuario = new javax.swing.JLabel();
        campoUsuario = new javax.swing.JTextField();
        labelSenha = new javax.swing.JLabel();
        campoSenha = new javax.swing.JPasswordField();
        labelConfirmarSenha = new javax.swing.JLabel();
        campoConfirmarSenha = new javax.swing.JPasswordField();
        botaoCancelar = new javax.swing.JButton();
        botaoCadastrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Usuário");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        labelNome.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        labelNome.setText("Nome");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(labelNome, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        getContentPane().add(campoNome, gridBagConstraints);

        labelEmail.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        labelEmail.setText("e-Mail");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(labelEmail, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        getContentPane().add(campoEmail, gridBagConstraints);

        labelUsuario.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        labelUsuario.setText("Usuário");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(labelUsuario, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        getContentPane().add(campoUsuario, gridBagConstraints);

        labelSenha.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        labelSenha.setText("Senha");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(labelSenha, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 140;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        getContentPane().add(campoSenha, gridBagConstraints);

        labelConfirmarSenha.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        labelConfirmarSenha.setText("Confirmar Senha");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(labelConfirmarSenha, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 140;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        getContentPane().add(campoConfirmarSenha, gridBagConstraints);

        botaoCancelar.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoCancelar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(20, 5, 5, 5);
        getContentPane().add(botaoCancelar, gridBagConstraints);

        botaoCadastrar.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoCadastrar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(20, 5, 5, 5);
        getContentPane().add(botaoCadastrar, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clicarBotaoCancelar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoCancelar
        int resp = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja cancelar o cadastro?", "Cancelar", JOptionPane.YES_NO_OPTION);
        
        if(resp == JOptionPane.YES_OPTION){
            this.dispose();
        }
    }//GEN-LAST:event_clicarBotaoCancelar

    private void clicarBotaoCadastrar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoCadastrar
        if(!existemDependencias()){
            String nome = campoNome.getText();
            String usuario = campoUsuario.getText();
            String email = campoEmail.getText();
            String senha = String.valueOf(campoSenha.getPassword());
            Usuario user = new Usuario(nome, usuario, email, senha);
            
            if(db.inserir(user)){
                JOptionPane.showMessageDialog(this, "Usuário cadastrado com sucesso.");
                this.reiniciar();
            } else {
                JOptionPane.showMessageDialog(this, "Falha ao cadastrar.");
            }
        }
    }//GEN-LAST:event_clicarBotaoCadastrar

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JanelaCadastroUsuario dialog = new JanelaCadastroUsuario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                     public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JPasswordField campoConfirmarSenha;
    private javax.swing.JTextField campoEmail;
    private javax.swing.JTextField campoNome;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JTextField campoUsuario;
    private javax.swing.JLabel labelConfirmarSenha;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JLabel labelUsuario;
    // End of variables declaration//GEN-END:variables

    private void reiniciar(){
        campoNome.setText("");
        campoEmail.setText("");
        campoUsuario.setText("");
        campoSenha.setText("");
        campoConfirmarSenha.setText("");
    }
    
    private boolean existemDependencias() {
        String msg = "Você esqueceu de preencher os \nseguintes campos obrigatórios:\n\n";

        boolean existemDependencias = false;
        String senha = String.valueOf(campoSenha.getPassword());
        String confirmaSenha = String.valueOf(campoConfirmarSenha.getPassword());

        if(campoNome.getText().equals("")) {
            msg += "- NOME\n";
            existemDependencias = true;
        }
        if(campoUsuario.getText().equals("")) {
            msg += "- USUÁRIO\n";
            existemDependencias = true;
        }
        if(senha.equals("")) {
            msg += "- SENHA\n";
            existemDependencias = true;
        }
        if(confirmaSenha.equals("")) {
            msg += "- CONFIRMAÇÃO DE SENHA\n";
            existemDependencias = true;
        }       
        
        if(existemDependencias){
            JOptionPane.showMessageDialog(this.getContentPane(), msg);
            return true;
        } else if(!senha.equals(confirmaSenha)){
            existemDependencias = true;
            JOptionPane.showMessageDialog(this, "A senha de confirmação não é igual a senha digitada.");
        }

        return existemDependencias;
    }
}
